const padding = "padding";
const success = "success";
const reject = "reject";
function myPromise(fn) {
    this.value; //成功的值
    this.reson; //失败的值
    this.state = padding; //状态
    this.fullback = []; //成功回调数组
    this.failback = []; //成功回调数组
    let that = this;
    //成功状态
    function fullFn(value) {
        that.value = value;
        that.state = success;
        that.fullback.map(item => item(value))
    }
    //失败状态
    function fildFn(reson) {
        that.reson = reson
        that.state = reject

        that.failback.map(item => item(reson))
    }

    fn(fullFn, fildFn)
}
myPromise.prototype.then = function (fn) {
    if (this.state == padding) {
        this.fullback.push(fn)
    }
    if (this.state == success) {
        fn(this.value)
    }
}
myPromise.prototype.catch = function (fn) {
    if (this.state == padding) {
        this.failback.push(fn)
    }
    if (this.state == reject) {
        fn(this.value)
    }
}

exports.myPromise = myPromise;